package cn.edu.neosoft.sunbo509.finefood.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neosoft.sunbo509.finefood.Bean.SearchBean;
import cn.edu.neosoft.sunbo509.finefood.R;

/**
 * Created by 小博 on 2017/11/13.
 */

public class SearchAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private List<SearchBean> list;

    public SearchAdapter(Context context) {
        this.context = context;
        layoutInflater=LayoutInflater.from(context);
    }
    public void setList(List<SearchBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
           SearchBean bean=list.get(position);
           ViewHolder holder1=(ViewHolder)holder;
        if(bean==null){
            return;
        }
        Picasso.with(context)
                .load(bean.getPic())
                .placeholder(null)
                .into(holder1.iv_picdetail);
        holder1.tv_foodname.setText(bean.getFoodname());
        holder1.tv_price.setText(bean.getPrice()+"");
        holder1.tv_intro.setText(bean.getIntro());
    }

    @Override
    public int getItemCount() {
        if(list==null)
            return 0;
        else
            return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_picdetail;
        private TextView tv_foodname;
        private TextView tv_intro;
        private TextView tv_price;
        public ViewHolder(View itemView) {
            super(itemView);
            iv_picdetail=(ImageView)itemView.findViewById(R.id.iv_picdetail2);
            tv_foodname=(TextView)itemView.findViewById(R.id.tv_foodname2);
            tv_intro=(TextView)itemView.findViewById(R.id.tv_intro2);
            tv_price=(TextView)itemView.findViewById(R.id.tv_price2);
        }
    }
}
